package com.doeis.uwork.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.doeis.uwork.entity.mongo.PrivateMsg;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.util.JSON;
import com.voland.core.ServiceException;
import com.voland.core.db.DaoException;
import com.voland.core.db.MongoDao;
import com.voland.core.db.Page;

@Singleton
public class PrivateMsgMongo extends MongoDao {
		

	/**
	 * 主要在手机界面上对人对指数显示
	 * @param myswitchbaord
	 * @param itemid
	 * @param otherUerid 对方用户编号
	 * @return
	 * @throws ServiceException
	 */
	public Page<PrivateMsg> findPrivateMsg(String myswitchbaord, Page page, String sourceId, 
			String otherUerid, String myUserid)throws DaoException{
		try{
			
			String con = " { sourceId: \""+sourceId+"\",  $or: [ { from:\""+otherUerid+"\" }, { to: \""+otherUerid+"\" } ] }  ";
			BasicDBObject query = (BasicDBObject)JSON.parse(con);	
			BasicDBObject sort = new BasicDBObject();
			sort.put("created_at", -1);
						
			return super.querySlice(myswitchbaord, PrivateMsg.class, page, query, sort);
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	
	
	/**
	 * 主要在手机界面上对人对指数显示
	 * @param myswitchbaord
	 * @param itemid
	 * @param otherUerid 对方用户编号
	 * @return
	 * @throws ServiceException
	 */
	public Page<PrivateMsg> findPrivateMsg(String myswitchbaord, Page page, PrivateMsg privateMsg)throws DaoException{
		try{
			
			StringBuffer con = new StringBuffer(" {  $or: [ { from:\""+privateMsg.getFrom()+"\" }, { to: \""+privateMsg.getFrom()+"\" } ]   ");
			if(privateMsg.getSourceId()!=null){
				con.append(" , sourceId: \""+privateMsg.getSourceId()+"\" ");
			}
			
			if(privateMsg.getImg()!=null){
				con.append(" , img: \""+privateMsg.getImg()+"\" ");
			}
			
			
			if(privateMsg.getMsceneType()!=null){
				con.append(" , msceneType: \""+privateMsg.getMsceneType()+"\" ");
			}
			
			con.append(" } ");
			BasicDBObject query = (BasicDBObject)JSON.parse(con.toString());	
			BasicDBObject sort = new BasicDBObject();
			sort.put("created_at", -1);
			return super.querySlice(myswitchbaord, PrivateMsg.class, page, query, sort);
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	/**
	 * 主要在手机界面上对人对指数显示
	 * @param myswitchbaord
	 * @param start //开始页数
	 * @param privateMsg
	 * @return
	 * @throws DaoException
	 */
	public List<PrivateMsg> findPrivateMsg(String myswitchbaord, Integer start,Integer count, PrivateMsg privateMsg)throws DaoException{
		try{
			
			StringBuffer con = new StringBuffer(" {  $or: [ { from:\""+privateMsg.getFrom()+"\" }, { to: \""+privateMsg.getFrom()+"\" } ]   ");
			if(privateMsg.getSourceId()!=null){
				con.append(" , sourceId: \""+privateMsg.getSourceId()+"\" ");
			}
			
			if(privateMsg.getImg()!=null){
				con.append(" , img: \""+privateMsg.getImg()+"\" ");
			}
			
			if(privateMsg.getMsceneType()!=null){
				con.append(" , msceneType: \""+privateMsg.getMsceneType()+"\" ");
			}
			
			con.append(" } ");
			BasicDBObject query = (BasicDBObject)JSON.parse(con.toString());	
			BasicDBObject sort = new BasicDBObject();
			sort.put("created_at", -1);
			
			DBCollection coll = getCollection(myswitchbaord, privateMsg.getClass());
			DBCursor cur;
			if (query != null) {
				cur = coll.find(query);
			} else {
				cur = coll.find();
			}
			
			if (sort != null) {
				cur.sort(sort);
			}
			
			if(start != null){
				if (start.intValue() == 0) {
					cur.limit(count);
				} else {
					if(count != null){
						cur.skip(start).limit(count);
					}else{
						cur.skip(start);
					}
				}
			}

			return toBeanList(cur, PrivateMsg.class);
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	
	
	/**
	 * 找出所有胶片（图片）
	 * @return
	 * @throws ServiceException
	 */
	public List<PrivateMsg> findPrivateMsgByImg(String sourceId, 
			String myswitchbaord, String myUserid) throws DaoException{
		try{			
			String con = " { sourceId: \""+sourceId+"\",$or: [ { from:\""+myUserid+"\" }, { to: \""+myUserid+"\" } ] }  ";
			BasicDBObject query = (BasicDBObject)JSON.parse(con);	
			
			String[] groupByColumns = {"img"};		
			BasicDBObject initial = new BasicDBObject();			
			String reduce = "function(doc, prev) { " +  
	                "prev.created_at = doc.created_at;" +  
	                "} ";  		
			return super.groupToList(myswitchbaord, PrivateMsg.class, groupByColumns, query, initial, reduce, null);
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	
	
	/**
	 * 找出与我相关的资源类型,资源可能是共享的模板，指数及拼图
	 * @return
	 * @throws ServiceException
	 */
	public List<PrivateMsg> findSourceBelongToMe(String myswitchbaord, String myUserid) throws DaoException{
		try{			
			String con = " { $or: [ { from:\""+myUserid+"\" }, { to: \""+myUserid+"\" } ] }  ";
			BasicDBObject query = (BasicDBObject)JSON.parse(con);	
			
			String[] groupByColumns = {"sourceId"};		
			BasicDBObject initial = new BasicDBObject();			
			String reduce = "function(doc, prev) { " +  
	                "prev.sourceType = doc.sourceType; " +  
	                "prev.shareid = doc.shareid;" +  
	                "} ";  		
						
			return super.groupToList(myswitchbaord, PrivateMsg.class, groupByColumns, query, initial, reduce, null);
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	
	/**
	 * 返回指数资源下，与我聊过天的人的编号
	 * @param myswitchbaord
	 * @param myUserid
	 * @return
	 * @throws ServiceException
	 */
	public Set<String> findUserChatToMe(String myswitchbaord, String myUserid, String sourceId) throws DaoException{
		try{		
			
			BasicDBObject query = new BasicDBObject("sourceId", sourceId);
			
			DBCollection coll = getCollection(myswitchbaord, PrivateMsg.class);
			List<String> froms = coll.distinct("from", query);			
			List<String> tos = coll.distinct("to", query);	
			
			Set<String> tmp = new HashSet<String>();
			tmp.addAll(froms);
			tmp.addAll(tos);
			
			return tmp;
		}catch(Exception e){
			throw new DaoException(e);
		}		
	}
	
	public static void main(String[] args) throws ServiceException {
		
		//李国利
		String myswitchbaord = "ff808081485600b001485601b8010000";
		String myUserid = "ff808081485600b001485601b8010001";
		String otherUerid = "402881ec48157e700148157e709a0001";
		String sourceid ="402881ef4a3d82f7014a3d8e5481000c";
		
		//小极
//		String myswitchbaord = "402881ec48157e700148157e709a0000";
//		String myUserid = "402881ec48157e700148157e709a0001";
//		String otherUerid = "ff808081485600b001485601b8010001";
//		String sourceid ="402881ef4a3d82f7014a3d8e5481000c";
		
		Injector injector = Guice.createInjector();
		PrivateMsgMongo mongo = injector.getInstance(PrivateMsgMongo.class);
		
//		Page<PrivateMsg> page = new Page<PrivateMsg>();
//		page = mongo.findPrivateMsg(myswitchbaord, page, sourceid, otherUerid, myUserid);
//		System.out.println(page.getResults());
		
//		List<PrivateMsg> findSourceBelongToMe = mongo.findSourceBelongToMe(myswitchbaord, myUserid);		
//		System.out.println(findSourceBelongToMe);
		
//		BasicDBList findUserChatToMe = mongo.findUserChatToMe(myswitchbaord, myUserid, sourceid);
//		System.out.println(findUserChatToMe);
		
	}
	
}
